package cn.iocoder.yudao.module.cmt.controller.admin.service;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.service.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.service.ServiceRequestDO;
import cn.iocoder.yudao.module.cmt.service.service.ServiceRequestService;

@Tag(name = "管理后台 - 服务申请")
@RestController
@RequestMapping("/cmt/service-request")
@Validated
public class ServiceRequestController {

    @Resource
    private ServiceRequestService serviceRequestService;

    @PostMapping("/create")
    @Operation(summary = "创建服务申请")
    @PreAuthorize("@ss.hasPermission('cmt:service-request:create')")
    public CommonResult<Long> createServiceRequest(@Valid @RequestBody ServiceRequestSaveReqVO createReqVO) {
        return success(serviceRequestService.createServiceRequest(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新服务申请")
    @PreAuthorize("@ss.hasPermission('cmt:service-request:update')")
    public CommonResult<Boolean> updateServiceRequest(@Valid @RequestBody ServiceRequestSaveReqVO updateReqVO) {
        serviceRequestService.updateServiceRequest(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除服务申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:service-request:delete')")
    public CommonResult<Boolean> deleteServiceRequest(@RequestParam("id") Long id) {
        serviceRequestService.deleteServiceRequest(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除服务申请")
    @PreAuthorize("@ss.hasPermission('cmt:service-request:delete')")
    public CommonResult<Boolean> deleteServiceRequestList(@RequestParam("ids") List<Long> ids) {
        serviceRequestService.deleteServiceRequestListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得服务申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:service-request:query')")
    public CommonResult<ServiceRequestRespVO> getServiceRequest(@RequestParam("id") Long id) {
        ServiceRequestDO serviceRequest = serviceRequestService.getServiceRequest(id);
        return success(BeanUtils.toBean(serviceRequest, ServiceRequestRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得服务申请分页")
    @PreAuthorize("@ss.hasPermission('cmt:service-request:query')")
    public CommonResult<PageResult<ServiceRequestRespVO>> getServiceRequestPage(@Valid ServiceRequestPageReqVO pageReqVO) {
        PageResult<ServiceRequestDO> pageResult = serviceRequestService.getServiceRequestPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ServiceRequestRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出服务申请 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:service-request:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportServiceRequestExcel(@Valid ServiceRequestPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceRequestDO> list = serviceRequestService.getServiceRequestPage(pageReqVO).getList();
        ExcelUtils.write(response, "服务申请.xls", "数据", ServiceRequestRespVO.class,
                        BeanUtils.toBean(list, ServiceRequestRespVO.class));
    }

}
